import Vue from 'vue'
import VueRouter from 'vue-router'
import routerIndex from './routerIndex.js'

Vue.use(VueRouter)

const routes = [
	{
		path: '/',
		name: 'login',
		component: () => import(/* webpackChunkName: "login" */ '../views/page/Login.vue')
	},
	{
		path: '/home',
		redirect: 'main',
		component: () => import(/* webpackChunkName: "main" */ '../views/common/Home.vue'),
		children: [
			...routerIndex,
			
			{
				path: "/error",
				component: () => import(/* webpackChunkName: "error" */ '../views/common/404.vue'),
				meta: {
					title: '404'
				}
			}
		]
	}
]

const router = new VueRouter({
  base: process.env.BASE_URL,
  routes
})
 /*判断此跳转路由的来源路由是否存在，存在的情况跳转到来源路由，否则跳转到404页面*/
 router.beforeEach((to, from, next) => {
  if (to.matched.length === 0) { 
    from.name ? next({
      name: from.name
    }) : next('/error');
  } else {
    next(); 
  }
});
export default router
